package com.atguigu.gmall.realtime.util;

import java.util.concurrent.LinkedBlockingDeque;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

/**
 * @author Felix
 * @date 2022/10/11
 * 获取线程池对象工具类
 * 双重校验锁解决单例设计模式懒汉式线程安全问题
 */
public class ThreadPoolUtil {
    /*
    //单例设计模式：饿汉式
    private static ThreadPoolExecutor threadPoolExecutor;

    static {
        threadPoolExecutor = new ThreadPoolExecutor(4,20,300, TimeUnit.SECONDS,
            new LinkedBlockingDeque<Runnable>(Integer.MAX_VALUE));
    }

    public static ThreadPoolExecutor getInstance(){
        return threadPoolExecutor;
    }*/
    //单例设计模式：懒汉式
    private static volatile ThreadPoolExecutor threadPoolExecutor;

    public static ThreadPoolExecutor getInstance(){
        if(threadPoolExecutor == null){
            synchronized(ThreadPoolUtil.class){
                if(threadPoolExecutor == null){
                    System.out.println("~~创建线程池对象~~");
                    threadPoolExecutor = new ThreadPoolExecutor(4,20,300, TimeUnit.SECONDS,
                        new LinkedBlockingDeque<Runnable>(Integer.MAX_VALUE));
                }
            }
        }
        return threadPoolExecutor;
    }
}
